/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.ability;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.bundle.AbilityInfo;

import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.player.VideoPlayer;
import com.yc.video.ui.view.BasisVideoController;

import org.yczbj.ycvideoplayer.ConstantVideo;
import org.yczbj.ycvideoplayer.ResourceTable;
import org.yczbj.ycvideoplayer.utils.ImageLoader;

/**
 * 3.2 竖屏全屏播放/横屏全屏播放
 *
 * @since 2021-05-10
 */
public class TestFullAbility extends BaseAbility implements Component.ClickedListener {
    private StackLayout stackLayout;
    private VideoPlayer mVideoPlayer;
    private Button mBtnTiny1;
    private Button mBtnTiny2;

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_full_video;
    }

    @Override
    protected void initComponent(Intent intent) {
        super.initComponent(intent);
        stackLayout = (StackLayout) findComponentById(ResourceTable.Id_stackLayout);
        mVideoPlayer = (VideoPlayer) findComponentById(ResourceTable.Id_video_player);
        mBtnTiny1 = (Button) findComponentById(ResourceTable.Id_btn_tiny_1);
        mBtnTiny2 = (Button) findComponentById(ResourceTable.Id_btn_tiny_2);

        mController = new BasisVideoController(this);
        mVideoPlayer.setDecorView(stackLayout);
        // 设置视频背景图
        ImageLoader.loadImage(getContext(), ResourceTable.Media_image_default, mController.getThumb());
        // 设置控制器
        mVideoPlayer.setController(mController);
        mVideoPlayer.setUrl(ConstantVideo.VideoPlayerList.get(0));
        mVideoPlayer.start();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mBtnTiny1.setClickedListener(this);
        mBtnTiny2.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_tiny_1:
                if (mVideoPlayer.getCurrentPlayerState() == ConstantKeys.PlayMode.MODE_TINY_WINDOW) {
                    return;
                }
                mVideoPlayer.startFullScreen();
                mController.portraitPlayer(true); // 是否处于竖屏播放
                break;
            case ResourceTable.Id_btn_tiny_2:
                ControlWrapper controlWrapper = mController.getControlWrapper();
                controlWrapper.toggleFullScreen(this);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
        // 竖屏全屏后再横屏全屏画面比例不正确
        if (mVideoPlayer.getCurrentPlayerState() == ConstantKeys.PlayMode.MODE_FULL_SCREEN
            && displayOrientation.equals(AbilityInfo.DisplayOrientation.LANDSCAPE)) {
            mVideoPlayer.refreshSurface();
        }
        if (displayOrientation.equals(AbilityInfo.DisplayOrientation.PORTRAIT)) {
            mBtnTiny1.setEnabled(true);
            mBtnTiny2.setEnabled(true);
        } else if (displayOrientation.equals(AbilityInfo.DisplayOrientation.LANDSCAPE)) {
            mBtnTiny1.setEnabled(false);
            mBtnTiny2.setEnabled(false);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mVideoPlayer != null) {
            mVideoPlayer.release();
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if (mVideoPlayer != null) {
            mVideoPlayer.pause();
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
        if (mVideoPlayer != null) {
            mVideoPlayer.resume();
        }
    }

    @Override
    protected void onBackPressed() {
        ControlWrapper.defaultValue();
        mController.portraitPlayer(false); // 恢复初始化值
        if (mVideoPlayer == null || !mVideoPlayer.onBackPressed()) {
            super.onBackPressed();
        }
    }
}
